home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr57 / mentc.zip / FILE.C < prev    next >
C/C++ Source or Header  |  1993-06-01  |  22KB  |  877 lines

  1. /*    FILE.C:   for MicroEMACS
  2.  
  3.     The routines in this file handle the reading, writing
  4.     and lookup of disk files.  All of details about the
  5.     reading and writing of the disk are in "fileio.c".
  6.  
  7. */
  8.  
  9. #include    <stdio.h>
  10. #include    "estruct.h"
  11. #include    "eproto.h"
  12. #include    "edef.h"
  13. #include    "elang.h"
  14. #if    BSD | SUN | V7
  15. #include    <sys/types.h>
  16. #include    <sys/stat.h>
  17. #endif
  18.  
  19. /*
  20.  * Read a file into the current
  21.  * buffer. This is really easy; all you do is
  22.  * find the name of the file, and call the standard
  23.  * "read a file into the current buffer" code.
  24.  * Bound to "C-X C-R".
  25.  */
  26. PASCAL NEAR fileread(f, n)
  27.  
  28. int f, n;    /* defualt and numeric arguments (unused) */
  29.  
  30. {
  31.     char *fname;    /* file name to read */
  32.  
  33.     if (restflag)        /* don't allow this command if restricted */
  34.         return(resterr());
  35.  
  36.     if ((fname = gtfilename(TEXT131)) == NULL)
  37. /*                              "Read file" */
  38.         return(FALSE);
  39.     return(readin(fname, TRUE));
  40. }
  41.  
  42. /*
  43.  * Insert a file into the current
  44.  * buffer. This is really easy; all you do it
  45.  * find the name of the file, and call the standard
  46.  * "insert a file into the current buffer" code.
  47.  * Bound to "C-X C-I".
  48.  */
  49. PASCAL NEAR insfile(f, n)
  50.  
  51. int f,n;    /* prefix flag and argument */
  52.  
  53. {
  54.     register int    s;
  55.     char *fname;    /* file name */
  56.     LINE *curline;
  57.  
  58.     if (restflag)        /* don't allow this command if restricted */
  59.         return(resterr());
  60.     if (curbp->b_mode&MDVIEW)      /* don't allow this command if  */
  61.         return(rdonly());    /* we are in read only mode    */
  62.  
  63.     if ((fname = gtfilename(TEXT132)) == NULL) 
  64. /*                              "Insert file" */
  65.         return(FALSE);
  66.     /*
  67.      * Save the local pointers to hold global ".", in case
  68.      * $yankflag is set to 1.  Insert-file always places the
  69.      * starting offset point at 0.  Hold *previous* line
  70.      * position, since the current line may be re-allocated.
  71.      */
  72.     if (yankflag)
  73.         curline = lback(curwp->w_dotp);
  74.  
  75.     s = ifile(fname);
  76.  
  77.     if (yankflag)
  78.         curwp->w_dotp = lforw(curline);
  79.  
  80.     return (s);
  81. }
  82.  
  83. /*
  84.  * Select a file for editing.
  85.  * Look around to see if you can find the
  86.  * fine in another buffer; if you can find it
  87.  * just switch to the buffer. If you cannot find
  88.  * the file, create a new buffer, read in the
  89.  * text, and switch to the new buffer.
  90.  * Bound to C-X C-F.
  91.  */
  92. PASCAL NEAR filefind(f, n)
  93.  
  94. int f,n;    /* prefix flag and argument */
  95.  
  96. {
  97.     char *fname;    /* file user wishes to find */    /* file name */
  98.  
  99.     if (restflag)        /* don't allow this command if restricted */
  100.         return(resterr());
  101.  
  102.     if ((fname = gtfilename(TEXT133)) == NULL) 
  103. /*                              "Find file" */
  104.         return(FALSE);
  105.     return(getfile(fname, TRUE));
  106. }
  107.  
  108. PASCAL NEAR viewfile(f, n)    /* visit a file in VIEW mode */
  109.  
  110. int f,n;    /* prefix flag and argument */
  111.  
  112. {
  113.     char *fname;    /* file user wishes to find */    /* file name */
  114.     register int s;    /* status return */
  115.  
  116.     if (restflag)        /* don't allow this command if restricted */
  117.         return(resterr());
  118.  
  119.     if ((fname = gtfilename(TEXT134)) == NULL) 
  120. /*                              "View file" */
  121.         return(FALSE);
  122.     s = getfile(fname, FALSE);
  123.     if (s) {    /* if we succeed, put it in view mode */
  124.         curwp->w_bufp->b_mode |= MDVIEW;
  125.         upmode();
  126.     }
  127.     return(s);
  128. }
  129.  
  130. #if    CRYPT
  131. PASCAL NEAR resetkey()    /* reset the encryption key if needed */
  132.  
  133. {
  134.     register int s; /* return status */
  135.  
  136.     /* turn off the encryption flag */
  137.     cryptflag = FALSE;
  138.  
  139.     /* if we are in crypt mode */
  140.     if (curbp->b_mode & MDCRYPT) {
  141.         if (curbp->b_key[0] == 0) {
  142.             s = setekey(FALSE, 0);
  143.             if (s != TRUE)
  144.                 return(s);
  145.         }
  146.  
  147.         /* let others know... */
  148.         cryptflag = TRUE;
  149.  
  150.         /* and set up the key to be used! */
  151.         /* de-encrypt it */
  152.         crypt((char *)NULL, 0);
  153.         crypt(curbp->b_key, strlen(curbp->b_key));
  154.  
  155.         /* re-encrypt it...seeding it to start */
  156.         crypt((char *)NULL, 0);
  157.         crypt(curbp->b_key, strlen(curbp->b_key));
  158.     }
  159.  
  160.     return(TRUE);
  161. }
  162. #endif
  163.  
  164. PASCAL NEAR getfile(fname, lockfl)
  165.  
  166. char fname[];        /* file name to find */
  167. int lockfl;        /* check the file for locks? */
  168.  
  169. {
  170.     register BUFFER *bp;
  171.     register LINE    *lp;
  172.     register int    i;
  173.     register int    s;
  174.     register int cmark;    /* current mark */
  175.     char bname[NBUFN];    /* buffer name to put file */
  176.  
  177. #if    MSDOS | OS2 | AOSVS | VMS | TOS
  178.     mklower(fname);            /* msdos isn't case sensitive */
  179. #endif
  180.     for (bp=bheadp; bp!=NULL; bp=bp->b_bufp) {
  181.         if ((bp->b_flag&BFINVS)==0 && strcmp(bp->b_fname, fname)==0) {
  182.             swbuffer(bp);
  183.             lp = curwp->w_dotp;
  184.             i = curwp->w_ntrows/2;
  185.             while (i-- && lback(lp)!=curbp->b_linep)
  186.                 lp = lback(lp);
  187.             curwp->w_linep = lp;
  188.             curwp->w_flag |= WFMODE|WFHARD;
  189.             mlwrite(TEXT135);
  190. /*                              "[Old buffer]" */
  191.             return(TRUE);
  192.         }
  193.     }
  194.     makename(bname, fname);         /* New buffer name.    */
  195.  
  196.     while ((bp=bfind(bname, FALSE, 0)) != NULL) {
  197.         /* old buffer name conflict code */
  198.         s = mlreply(TEXT136, bname, NBUFN);
  199. /*                          "Buffer name: " */
  200.         if (s == ABORT)         /* ^G to just quit    */
  201.             return(s);
  202.         if (s == FALSE) {        /* CR to clobber it    */
  203.             makename(bname, fname);
  204.             break;
  205.         }
  206.     }
  207.     if (bp==NULL && (bp=bfind(bname, TRUE, 0))==NULL) {
  208.         mlwrite(TEXT137);
  209. /*                      "Cannot create buffer" */
  210.         return(FALSE);
  211.     }
  212.  
  213.     if (--curbp->b_nwnd == 0) {        /* Undisplay.        */
  214.         curbp->b_dotp = curwp->w_dotp;
  215.         curbp->b_doto = curwp->w_doto;
  216.         for (cmark = 0; cmark < NMARKS; cmark++) {
  217.             curbp->b_markp[cmark] = curwp->w_markp[cmark];
  218.             curbp->b_marko[cmark] = curwp->w_marko[cmark];
  219.         }
  220.         curbp->b_fcol = curwp->w_fcol;
  221.     }
  222.     curbp = bp;                /* Switch to it.    */
  223.     curwp->w_bufp = bp;
  224.     curbp->b_nwnd++;
  225.     return(readin(fname, lockfl));        /* Read it in.        */
  226. }
  227.  
  228. /*
  229.     Read file "fname" into the current buffer, blowing away any text
  230.     found there.  Called by both the read and find commands.  Return
  231.     the final status of the read.  Also called by the mainline, to
  232.     read in a file specified on the command line as an argument. 
  233.     The command in $readhook is called after the buffer is set up
  234.     and before it is read. 
  235. */
  236.  
  237. PASCAL NEAR readin(fname, lockfl)
  238.  
  239. char    fname[];    /* name of file to read */
  240. int    lockfl;        /* check for file locks? */
  241.  
  242. {
  243.     register LINE *lp1;
  244.     register LINE *lp2;
  245.     register int i;
  246.     register WINDOW *wp;
  247.     register BUFFER *bp;
  248.     register int s;
  249.     register int nbytes;
  250.     register int nline;
  251.     register int cmark;    /* current mark */
  252.     char mesg[NSTRING];
  253.  
  254. #if    FILOCK
  255.     if (lockfl && lockchk(fname) == ABORT)
  256.         return(ABORT);
  257. #endif
  258.  
  259.     bp = curbp;                /* Cheap.        */
  260.     if ((s=bclear(bp)) != TRUE)        /* Might be old.    */
  261.         return(s);
  262.     bp->b_flag &= ~(BFINVS|BFCHG);
  263.     strcpy(bp->b_fname, fname);
  264.  
  265.     /* let a user macro get hold of things...if he wants */
  266.     execkey(&readhook, FALSE, 1);
  267.  
  268. #if    CRYPT
  269.     /* set up for decryption */
  270.     s = resetkey();
  271.     if (s != TRUE)
  272.         return(s);
  273. #endif
  274.  
  275.     /* turn off ALL keyboard translation in case we get a dos error */
  276.     TTkclose();
  277.  
  278.     if ((s=ffropen(fname)) == FIOERR)    /* Hard file open.    */
  279.         goto out;
  280.  
  281.     if (s == FIOFNF) {            /* File not found.    */
  282.         mlwrite(TEXT138);
  283. /*                      "[New file]" */
  284.         goto out;
  285.     }
  286.  
  287.     /* read the file in */
  288.     mlwrite(TEXT139);
  289. /*              "[Reading file]" */
  290.     nline = 0;
  291.     while ((s=ffgetline()) == FIOSUC) {
  292.         nbytes = strlen(fline);
  293.         if ((lp1=lalloc(nbytes)) == NULL) {
  294.             s = FIOMEM;        /* Keep message on the    */
  295.             break;            /* display.        */
  296.         }
  297.         lp2 = lback(curbp->b_linep);
  298.         lp2->l_fp = lp1;
  299.         lp1->l_fp = curbp->b_linep;
  300.         lp1->l_bp = lp2;
  301.         curbp->b_linep->l_bp = lp1;
  302.         for (i=0; i<nbytes; ++i)
  303.             lputc(lp1, i, fline[i]);
  304.         ++nline;
  305.     }
  306.     ffclose();                /* Ignore errors.    */
  307.     strcpy(mesg, "[");
  308.     if (s==FIOERR) {
  309.         strcat(mesg, TEXT141);
  310. /*                           "I/O ERROR, " */
  311.         curbp->b_flag |= BFTRUNC;
  312.     }
  313.     if (s == FIOMEM) {
  314.         strcat(mesg, TEXT142);
  315. /*                           "OUT OF MEMORY, " */
  316.         curbp->b_flag |= BFTRUNC;
  317.     }
  318.     strcat(mesg, TEXT140);
  319. /*                   "Read " */
  320.     strcat(mesg, int_asc(nline));
  321.     strcat(mesg, TEXT143);
  322. /*                   " line" */
  323.     if (nline > 1)
  324.         strcat(mesg, "s");
  325.     strcat(mesg, "]");
  326.     mlwrite(mesg);
  327.  
  328. out:
  329.     TTkopen();    /* open the keyboard again */
  330.     for (wp=wheadp; wp!=NULL; wp=wp->w_wndp) {
  331.         if (wp->w_bufp == curbp) {
  332.             wp->w_linep = lforw(curbp->b_linep);
  333.             wp->w_dotp  = lforw(curbp->b_linep);
  334.             wp->w_doto  = 0;
  335.             for (cmark = 0; cmark < NMARKS; cmark++) {
  336.                 wp->w_markp[cmark] = NULL;
  337.                 wp->w_marko[cmark] = 0;
  338.             }
  339.             wp->w_flag |= WFMODE|WFHARD;
  340.         }
  341.     }
  342.     if (s == FIOERR || s == FIOFNF)     /* False if error.    */
  343.         return(FALSE);
  344.     return(TRUE);
  345. }
  346.  
  347. /*
  348.  * Take a file name, and from it
  349.  * fabricate a buffer name. This routine knows
  350.  * about the syntax of file names on the target system.
  351.  * I suppose that this information could be put in
  352.  * a better place than a line of code.
  353.  * Returns a pointer into fname indicating the end of the file path; i.e.,
  354.  * 1 character BEYOND the path name.
  355.  */
  356. char *PASCAL NEAR makename(bname, fname)
  357.  
  358. char *bname;
  359. char *fname;
  360.  
  361. {
  362.     register char *cp1;
  363.     register char *cp2;
  364.     register char *pathp;
  365.  
  366. #if     AOSVS | MV_UX
  367.         resolve_full_pathname(fname, fname);
  368.         mklower(fname);   /* aos/vs not case sensitive */
  369. #endif
  370.     cp1 = &fname[0];
  371.     while (*cp1 != 0)
  372.         ++cp1;
  373.  
  374. #if    AMIGA
  375.     while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!='/')
  376.         --cp1;
  377. #endif
  378. #if     AOSVS | MV_UX
  379.         while (cp1!=&fname[0] && cp1[-1]!=':')
  380.                 --cp1;
  381. #endif
  382. #if    VMS
  383.     while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!=']')
  384.         --cp1;
  385. #endif
  386. #if    WINNT
  387.     while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!='\\')
  388.         --cp1;
  389. #endif
  390. #if    MSDOS | OS2
  391.     while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!='\\'&&cp1[-1]!='/')
  392.         --cp1;
  393. #endif
  394. #if    TOS
  395.     while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!='\\')
  396.         --cp1;
  397. #endif
  398. #if    FINDER
  399.     while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!='\\'&&cp1[-1]!='/')
  400.         --cp1;
  401. #endif
  402. #if    V7 | USG | SMOS | HPUX | BSD | SUN | XENIX | AVIION
  403.     while (cp1!=&fname[0] && cp1[-1]!='/')
  404.         --cp1;
  405. #endif
  406. #if WMCS
  407.     while (cp1!=&fname[0] && cp1[-1]!='_' && cp1[-1]!='/')
  408.         --cp1;
  409. #endif
  410.     /* cp1 is pointing to the first real filename char */
  411.     pathp = cp1;
  412.  
  413.     cp2 = &bname[0];
  414.     while (cp2!=&bname[NBUFN-1] && *cp1!=0 && *cp1!=';')
  415.         *cp2++ = *cp1++;
  416.     *cp2 = 0;
  417.  
  418.     return(pathp);
  419. }
  420.  
  421. PASCAL NEAR unqname(name)    /* make sure a buffer name is unique */
  422.  
  423. char *name;    /* name to check on */
  424.  
  425. {
  426.     register char *sp;
  427.  
  428.     /* check to see if it is in the buffer list */
  429.     while (bfind(name, 0, FALSE) != NULL) {
  430.  
  431.         /* go to the end of the name */
  432.         sp = name;
  433.         while (*sp)
  434.             ++sp;
  435.         if (sp == name || (*(sp-1) <'0' || *(sp-1) > '8')) {
  436.             *sp++ = '0';
  437.             *sp = 0;
  438.         } else
  439.               *(--sp) += 1;
  440.     }
  441.  
  442.     return (0);
  443. }
  444.  
  445. /*
  446.  * Ask for a file name, and write the
  447.  * contents of the current buffer to that file.
  448.  * Update the remembered file name and clear the
  449.  * buffer changed flag. This handling of file names
  450.  * is different from the earlier versions, and
  451.  * is more compatable with Gosling EMACS than
  452.  * with ITS EMACS. Bound to "C-X C-W" for writing
  453.  * and ^X^A for appending.
  454.  */
  455.  
  456. PASCAL NEAR filewrite(f, n)
  457.  
  458. int f, n;    /* emacs arguments */
  459.  
  460. {
  461.     register int s;
  462.     char fname[NFILEN];
  463.  
  464.     if (restflag)        /* don't allow this command if restricted */
  465.         return(resterr());
  466.     if ((s=mlreply(TEXT144, fname, NFILEN)) != TRUE)
  467. /*                     "Write file: " */
  468.         return(s);
  469.     if ((s=writeout(fname, "w")) == TRUE) {
  470.         strcpy(curbp->b_fname, fname);
  471.         curbp->b_flag &= ~BFCHG;
  472.         /* Update mode lines.    */
  473.         upmode();
  474.     }
  475.     return(s);
  476. }
  477.  
  478. PASCAL NEAR fileapp(f, n)    /* append file */
  479.  
  480. int f, n;    /* emacs arguments */
  481.  
  482. {
  483.     register int s;
  484.     char fname[NFILEN];
  485.  
  486.     if (restflag)        /* don't allow this command if restricted */
  487.         return(resterr());
  488.     if ((s=mlreply(TEXT218, fname, NFILEN)) != TRUE)
  489. /*                     "Append file: " */
  490.         return(s);
  491.     if ((s=writeout(fname, "a")) == TRUE) {
  492.         curbp->b_flag &= ~BFCHG;
  493.         /* Update mode lines.    */
  494.         upmode();
  495.     }
  496.     return(s);
  497. }
  498.  
  499. /*
  500.  * Save the contents of the current
  501.  * buffer in its associatd file. Do nothing
  502.  * if nothing has changed (this may be a bug, not a
  503.  * feature). Error if there is no remembered file
  504.  * name for the buffer. Bound to "C-X C-S". May
  505.  * get called by "C-Z".
  506.  */
  507. PASCAL NEAR filesave(f, n)
  508.  
  509. int f,n;    /* prefix flag and argument */
  510.  
  511. {
  512.     register int s;
  513.  
  514.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  515.         return(rdonly());    /* we are in read only mode    */
  516.     if ((curbp->b_flag&BFCHG) == 0)     /* Return, no changes.    */
  517.         return(TRUE);
  518.     if (curbp->b_fname[0] == 0) {        /* Must have a name.    */
  519.         mlwrite(TEXT145);
  520. /*                      "No file name" */
  521.         return(FALSE);
  522.     }
  523.  
  524.     /* complain about truncated files */
  525.     if ((curbp->b_flag&BFTRUNC) != 0) {
  526.         if (mlyesno(TEXT146) == FALSE) {
  527. /*                          "Truncated file..write it out" */
  528.             mlwrite(TEXT8);
  529. /*                              "[Aborted]" */
  530.             return(FALSE);
  531.         }
  532.     }
  533.  
  534.     /* complain about narrowed buffers */
  535.     if ((curbp->b_flag&BFNAROW) != 0) {
  536.         if (mlyesno(TEXT147) == FALSE) {
  537. /*                          "Narrowed Buffer..write it out" */
  538.             mlwrite(TEXT8);
  539. /*                              "[Aborted]" */
  540.             return(FALSE);
  541.         }
  542.     }
  543.  
  544.     if ((s=writeout(curbp->b_fname, "w")) == TRUE) {
  545.         curbp->b_flag &= ~BFCHG;
  546.         /* Update mode lines.    */
  547.         upmode();
  548.     }
  549.     return(s);
  550. }
  551.  
  552. /*
  553.  * This function performs the details of file writing. It uses
  554.  * the file management routines in the "fileio.c" package. The
  555.  * number of lines written is displayed. Several errors are
  556.  * posible, and cause writeout to return a FALSE result. When
  557.  * $ssave is TRUE,  the buffer is written out to a temporary
  558.  * file, and then the old file is unlinked and the temporary
  559.  * renamed to the original name.  Before the file is written,
  560.  * a user specifyable routine (in $writehook) can be run.
  561.  */
  562.  
  563. PASCAL NEAR writeout(fn, mode)
  564.  
  565. char *fn;    /* name of file to write current buffer to */
  566. char *mode;    /* mode to open file (w = write a = append) */
  567. {
  568.     register LINE *lp;    /* line to scan while writing */
  569.     register char *sp;    /* temporary string pointer */
  570.     register int nline;    /* number of lines written */
  571.     int status;        /* return status */
  572.     int sflag;        /* are we safe saving? */
  573.     char tname[NSTRING];    /* temporary file name */
  574.     char buf[NSTRING];    /* message buffer */
  575. #if    BSD | SUN | V7
  576.     struct stat st;        /* we need info about the file permisions */
  577. #endif
  578.  
  579.     /* let a user macro get hold of things...if he wants */
  580.     execkey(&writehook, FALSE, 1);
  581.  
  582.     /* determine if we will use the save method */
  583.     sflag = FALSE;
  584.     if (ssave && fexist(fn) && *mode == 'w')
  585.         sflag = TRUE;
  586.  
  587. #if    CRYPT
  588.     /* set up for file encryption */
  589.     status = resetkey();
  590.     if (status != TRUE)
  591.         return(status);
  592. #endif
  593.  
  594.     /* turn off ALL keyboard translation in case we get a dos error */
  595.     TTkclose();
  596.  
  597.     /* Perform Safe Save..... */
  598.     if (sflag) {
  599.         /* duplicate original file name, and find where to trunc it */
  600.         sp = tname + (makename(tname, fn) - fn) + 1;
  601.         strcpy(tname, fn);
  602.  
  603.         /* create a unique name, using random numbers */
  604.         do {
  605.             *sp = 0;
  606.             strcat(tname, int_asc(ernd() & 0xffff));
  607.         } while(fexist(tname));
  608.  
  609.         /* open the temporary file */
  610. #if     AOSVS
  611.                 status = ffwopen(fn, "w", tname);
  612. #else
  613.         status = ffwopen(tname, "w");
  614. #endif
  615.     } else
  616. #if     AOSVS
  617.                 status = ffwopen(fn, mode, NULL);
  618. #else
  619.         status = ffwopen(fn, mode);
  620. #endif
  621.  
  622.     /* if the open failed.. clean up and abort */
  623.     if (status != FIOSUC) {
  624.         TTkopen();
  625.         return(FALSE);
  626.     }
  627.  
  628.     /* write the current buffer's lines to the open disk file */
  629.     mlwrite(TEXT148);    /* tell us that we're writing */
  630. /*              "[Writing...]" */
  631.     lp = lforw(curbp->b_linep);    /* start at the first line.    */
  632.     nline = 0;            /* track the Number of lines    */
  633.     while (lp != curbp->b_linep) {
  634.         if ((status = ffputline(&lp->l_text[0], llength(lp))) != FIOSUC)
  635.             break;
  636.         ++nline;
  637.         lp = lforw(lp);
  638.     }
  639.  
  640.  
  641.     /* report on status of file write */
  642.     *buf = 0;
  643.     status |= ffclose();
  644.     if (status == FIOSUC) {
  645.         /* report on success (or lack therof) */
  646.         strcpy(buf, TEXT149);
  647. /*                          "[Wrote " */
  648.         strcat(buf, int_asc(nline));
  649.         strcat(buf, TEXT143);
  650. /*                          " line" */
  651.         if (nline > 1)
  652.             strcat(buf, "s");
  653.  
  654.         if (sflag) {
  655. #if    BSD | SUN | V7
  656.             /* get the permisions on the original file */
  657.             stat(fn, &st);
  658. #endif
  659.             /* erase original file */
  660.             /* rename temporary file to original name */
  661.             if (unlink(fn) == 0 && rename(tname, fn) == 0) {
  662. #if    BSD | SUN | V7
  663.                 chmod(fn, (int)st.st_uid, (int)st.st_gid);
  664.                 chmod(fn, (int)st.st_mode);
  665. #else
  666.                 ;
  667. #endif
  668.             } else {
  669.                 strcat(buf, TEXT150);
  670. /*                                          ", saved as " */
  671.                 strcat(buf, tname);
  672.                 status = FIODEL;        /* failed */
  673.             }
  674.         }
  675.         strcat(buf, "]");
  676.         mlwrite(buf);
  677.     }
  678.  
  679.     /* reopen the keyboard, and return our status */
  680.     TTkopen();
  681.     return(status == FIOSUC);
  682. }
  683.  
  684. /*
  685.  * The command allows the user
  686.  * to modify the file name associated with
  687.  * the current buffer. It is like the "f" command
  688.  * in UNIX "ed". The operation is simple; just zap
  689.  * the name in the BUFFER structure, and mark the windows
  690.  * as needing an update. You can type a blank line at the
  691.  * prompt if you wish.
  692.  */
  693.  
  694. PASCAL NEAR filename(f, n)
  695.  
  696. int f,n;    /* prefix flag and argument */
  697.  
  698. {
  699.     register int    s;
  700.     char        fname[NFILEN];
  701.  
  702.     if (restflag)        /* don't allow this command if restricted */
  703.         return(resterr());
  704.     if ((s=mlreply(TEXT151, fname, NFILEN)) == ABORT)
  705. /*                     "Name: " */
  706.         return(s);
  707.     if (s == FALSE)
  708.         strcpy(curbp->b_fname, "");
  709.     else
  710.         strcpy(curbp->b_fname, fname);
  711.     /* Update mode lines.    */
  712.     upmode();
  713.     curbp->b_mode &= ~MDVIEW;      /* no longer read only mode */
  714.     return(TRUE);
  715. }
  716.  
  717. /*
  718.  * Insert file "fname" into the current
  719.  * buffer, Called by insert file command. Return the final
  720.  * status of the read.
  721.  */
  722. PASCAL NEAR ifile(fname)
  723. char    fname[];
  724. {
  725.     register LINE *lp0;
  726.     register LINE *lp1;
  727.     register LINE *lp2;
  728.     register int i;
  729.     register BUFFER *bp;
  730.     register int s;
  731.     register int nbytes;
  732.     register int nline;
  733.     int cmark;    /* current mark */
  734.     char mesg[NSTRING];
  735.  
  736.     bp = curbp;                /* Cheap.        */
  737.     bp->b_flag |= BFCHG;            /* we have changed    */
  738.     bp->b_flag &= ~BFINVS;            /* and are not temporary*/
  739.     if ((s=ffropen(fname)) == FIOERR)    /* Hard file open.    */
  740.         goto out;
  741.     if (s == FIOFNF) {            /* File not found.    */
  742.         mlwrite(TEXT152);
  743. /*                      "[No such file]" */
  744.         return(FALSE);
  745.     }
  746.     mlwrite(TEXT153);
  747. /*              "[Inserting file]" */
  748.  
  749. #if    CRYPT
  750.     s = resetkey();
  751.     if (s != TRUE)
  752.         return(s);
  753. #endif
  754.     /* back up a line and save the mark here */
  755.     curwp->w_dotp = lback(curwp->w_dotp);
  756.     curwp->w_doto = 0;
  757.     for (cmark = 0; cmark < NMARKS; cmark++) {
  758.         curwp->w_markp[cmark] = curwp->w_dotp;
  759.         curwp->w_marko[cmark] = 0;
  760.     }
  761.  
  762.     nline = 0;
  763.     while ((s=ffgetline()) == FIOSUC) {
  764.         nbytes = strlen(fline);
  765.         if ((lp1=lalloc(nbytes)) == NULL) {
  766.             s = FIOMEM;        /* Keep message on the    */
  767.             break;            /* display.        */
  768.         }
  769.         lp0 = curwp->w_dotp;  /* line previous to insert */
  770.         lp2 = lp0->l_fp;    /* line after insert */
  771.  
  772.         /* re-link new line between lp0 and lp2 */
  773.         lp2->l_bp = lp1;
  774.         lp0->l_fp = lp1;
  775.         lp1->l_bp = lp0;
  776.         lp1->l_fp = lp2;
  777.  
  778.         /* and advance and write out the current line */
  779.         curwp->w_dotp = lp1;
  780.         for (i=0; i<nbytes; ++i)
  781.             lputc(lp1, i, fline[i]);
  782.         ++nline;
  783.     }
  784.     ffclose();                /* Ignore errors.    */
  785.     curwp->w_markp[0] = lforw(curwp->w_markp[0]);
  786.     strcpy(mesg, "[");
  787.     if (s==FIOERR) {
  788.         strcat(mesg, TEXT141);
  789. /*                           "I/O ERROR, " */
  790.         curbp->b_flag |= BFTRUNC;
  791.     }
  792.     if (s == FIOMEM) {
  793.         strcat(mesg, TEXT142);
  794. /*                           "OUT OF MEMORY, " */
  795.         curbp->b_flag |= BFTRUNC;
  796.     }
  797.     strcat(mesg, TEXT154);
  798. /*                   "Inserted " */
  799.     strcat(mesg, int_asc(nline));
  800.     strcat(mesg, TEXT143);
  801. /*                   " line" */
  802.     if (nline > 1)
  803.         strcat(mesg, "s");
  804.     strcat(mesg, "]");
  805.     mlwrite(mesg);
  806.  
  807. out:
  808.     /* advance to the next line and mark the window for changes */
  809.     curwp->w_dotp = lforw(curwp->w_dotp);
  810.     curwp->w_flag |= WFHARD | WFMODE;
  811.  
  812.     /* copy window parameters back to the buffer structure */
  813.     curbp->b_dotp = curwp->w_dotp;
  814.     curbp->b_doto = curwp->w_doto;
  815.     for (cmark = 0; cmark < NMARKS; cmark++) {
  816.         curbp->b_markp[cmark] = curwp->w_markp[cmark];
  817.         curbp->b_marko[cmark] = curwp->w_marko[cmark];
  818.     }
  819.     curbp->b_fcol = curwp->w_fcol;
  820.  
  821.     if (s == FIOERR)            /* False if error.    */
  822.         return(FALSE);
  823.     return(TRUE);
  824. }
  825.  
  826. /*    show-files    Bring up a fake buffer and list the
  827.             names of all the files in a given directory
  828. */
  829.  
  830. PASCAL NEAR showfiles(f, n)
  831.  
  832. int f,n;    /* prefix flag and argument */
  833.  
  834. {
  835.     register BUFFER *dirbuf;/* buffer to put file list into */
  836.     char outseq[NSTRING];    /* output buffer for file names */
  837.     char *sp;        /* output ptr for file names */
  838.     char mstring[NSTRING];    /* string to match cmd names to */
  839.     int status;        /* status return */
  840.  
  841.     /* ask what directory mask to search */
  842.     status = mlreply("Directory to show: ", mstring, NSTRING - 1);
  843.     if (status == ABORT)
  844.         return(status);
  845.  
  846.     /* get a buffer for the file list */
  847.     dirbuf = bfind("File List", TRUE, 0);
  848.     if (dirbuf == NULL || bclear(dirbuf) == FALSE) {
  849.         mlwrite("Can not display file list");
  850. /*            "Can not display function list" */
  851.         return(FALSE);
  852.     }
  853.  
  854.     /* let us know this is in progress */
  855.     mlwrite("[Building File List]");
  856.  
  857.     /* get the first file name */
  858.     sp = getffile(mstring);
  859.  
  860.     while (sp) {
  861.  
  862.         /* add a name to the buffer */
  863.         strcpy(outseq, sp);
  864.         if (addline(dirbuf, outseq) != TRUE)
  865.             return(FALSE);
  866.  
  867.         /* and get the next name */
  868.         sp = getnfile();
  869.     }
  870.  
  871.     /* display the list */
  872.     wpopup(dirbuf);
  873.     mlerase();    /* clear the mode line */
  874.     return(TRUE);
  875. }
  876.  
  877.